package anli;


public class anli6 {
    public static void main(String[] args) {
        System.out.println("加密后的结果是:" + passsword(1234567));
    }

    public static String passsword(int number) {
        //把密码拆分成一个个数组，才可以加密
        int[] numbers = split(number);
        //numbers=[1,9,8,3]

        //2.遍历这个数组中每个数组，对其加密
        for (int i = 0; i < numbers.length; i++) {
            //i=0 1 2 3
            numbers[i] = (numbers[i] + 5) % 10;
        }

        //3.对数组进行反转
        reverse(numbers);

        //把这些加密的数字拼接起来作为加密后的结果返回
        String data = "";
        for (int i = 0; i < numbers.length; i++) {
            data += numbers[i];
        }
        return data;
    }

    public static void reverse(int[] numbers) {
        //反转数组
        for (int i = 0, j = numbers.length - 1; i < numbers.length; i++, j--) {
            int temp = numbers[j];
            numbers[j] = numbers[i];
            numbers[i] = temp;

        }
    }

    public static int[] split(int number) {
        // 这个是写死的方式
        int[] numbers = new int[number];
        numbers[0] = number / 1000;
        numbers[1] = (number / 100) % 10;
        numbers[2] = (number / 10) % 10;
        numbers[3] = number % 10;

        // 扩展：动态计算 number 的位数，并将每一位放入 numbers 数组
//        int count = number <= 0 ? 1 : 0;
//        int numberTemp = number;
//        while (numberTemp > 0) {
//            numberTemp /= 10;
//            count++;
//        }
//
//        int[] numbers = new int[count];
//        for (int i = numbers.length - 1; i >= 0; i--) {
//            numbers[i] = number % 10;
//            number /= 10;
//        }
        return numbers;

    }
}



